<?php
namespace app\admin\common\controller;


use think\Controller;
use app\admin\common\model\Menu;
use think\facade\View;
use think\facade\Session;
use app\admin\common\service\RbacSrv;
use think\facade\Request;

/*
 * 后台控制器基类
 */
class Base extends Controller
{
    protected $beforeActionList = [
        'checkLogin'    =>  ['except'=>['login','imgcode','logindo','logout']],
        'checkPower'    =>  ['except'=>['login','imgcode','logindo','logout']],
    ];

    public function initialize()
    {
        $menu = new Menu();
        $menuDatas = $menu->getMenuRecursion();
        

        //赋值全局模板变量
        View::share('menuDatas',$menuDatas);
    }

    public function checkPower()
    {
        $url = Request::url();
        $rbac = new RbacSrv();
        $isPower = $rbac->checkPower($url);

        $adminInfo = Session::get('admin');
        if ($adminInfo['id'] != config('app.rbac_admin_id')) {
            if (false === $isPower) {
                $this->error('你没有权限操作');
            }
        }
    }

    public function checkLogin()
    {
        $adminInfo = Session::get('admin');

        if (empty($adminInfo)) {
            $this->redirect('admin/Common/login');
        }

        $viewData = [
            'adminInfo'=>$adminInfo,
        ];

        View::share($viewData);
    }
}